home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2005 June
/
ccd0605.iso
/
Software
/
Freeware
/
Programare
/
highlight
/
highlight-W32GUI-2.2-10b-Setup.exe
/
{app}
/
src
/
rtfgenerator.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2005-03-20
|
8KB
|
274 lines
/***************************************************************************
rtfcode.cpp - description
-------------------
begin : Die Jul 9 2002
copyright : (C) 2002 by AndrΘ Simon
email : andre.simon1@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "rtfgenerator.h"
using namespace std;
namespace highlight {
string RtfGenerator::formatStyleAttributes( const ElementStyle & col)
{
stringstream s;
s << "\\red"<< col.getColour().getRTFRedValue()
<< "\\green"<<col.getColour().getRTFGreenValue()
<< "\\blue"<<col.getColour().getRTFBlueValue()
<< ";";
return s.str();
}
string RtfGenerator::getOpenTag(int styleNumber,const ElementStyle & elem)
{
ostringstream s;
s << "{\\cf"<<(styleNumber+1)<<"{";
if (elem.isBold()) s << "\\b ";
if (elem.isItalic()) s << "\\i ";
if (elem.isUnderline()) s << "\\ul ";
return s.str();
}
string RtfGenerator::getCloseTag(const ElementStyle &elem)
{
ostringstream s;
if (elem.isBold()) s << "\\b0 ";
if (elem.isItalic()) s << "\\i0 ";
if (elem.isUnderline()) s << "\\ul0 ";
s << "}}";
return s.str();
}
RtfGenerator::RtfGenerator(const string &colourTheme)
: CodeGenerator( colourTheme)
{
styleTagOpen.push_back(getOpenTag(STANDARD, docStyle.getDefaultStyle()));
styleTagOpen.push_back(getOpenTag(STRING, docStyle.getStringStyle()));
styleTagOpen.push_back(getOpenTag(NUMBER, docStyle.getNumberStyle()));
styleTagOpen.push_back(getOpenTag(SL_COMMENT, docStyle.getSingleLineCommentStyle()));
styleTagOpen.push_back(getOpenTag(ML_COMMENT_BEGIN,docStyle.getCommentStyle()));
styleTagOpen.push_back(getOpenTag(ESC_CHAR, docStyle.getEscapeCharStyle()));
styleTagOpen.push_back(getOpenTag(DIRECTIVE_LINE, docStyle.getDirectiveStyle()));
styleTagOpen.push_back(getOpenTag(DIRECTIVE_STRING, docStyle.getDirectiveStringStyle()));
styleTagOpen.push_back(getOpenTag(LINENUMBER, docStyle.getLineStyle()));
styleTagOpen.push_back(getOpenTag(SYMBOL, docStyle.getSymbolStyle()));
styleTagClose.push_back(getCloseTag(docStyle.getDefaultStyle()));
styleTagClose.push_back(getCloseTag(docStyle.getStringStyle()));
styleTagClose.push_back(getCloseTag(docStyle.getNumberStyle()));
styleTagClose.push_back(getCloseTag(docStyle.getSingleLineCommentStyle()));
styleTagClose.push_back(getCloseTag(docStyle.getCommentStyle()));
styleTagClose.push_back(getCloseTag(docStyle.getEscapeCharStyle()));
styleTagClose.push_back(getCloseTag(docStyle.getDirectiveStyle()));
styleTagClose.push_back(getCloseTag(docStyle.getDirectiveStringStyle()));
styleTagClose.push_back(getCloseTag(docStyle.getLineStyle()));
styleTagClose.push_back(getCloseTag(docStyle.getSymbolStyle()));
newLineTag = "\\par\\pard\n";
spacer = " ";
}
RtfGenerator::RtfGenerator()
{}
RtfGenerator::~RtfGenerator()
{}
string RtfGenerator::getHeader(const string & title)
{
return string();
}
void RtfGenerator::printBody()
{
*out << "{\\rtf1\\ansi\\uc0 \\deff1"
<< "{\\fonttbl{\\f1\\fmodern\\fprq1\\fcharset0 Courier;}}"
<< "{\\colortbl;";
*out << formatStyleAttributes(docStyle.getDefaultStyle());
*out << formatStyleAttributes(docStyle.getStringStyle());
*out << formatStyleAttributes(docStyle.getNumberStyle());
*out << formatStyleAttributes(docStyle.getSingleLineCommentStyle());
*out << formatStyleAttributes(docStyle.getCommentStyle());
*out << formatStyleAttributes(docStyle.getEscapeCharStyle());
*out << formatStyleAttributes(docStyle.getDirectiveStyle());
*out << formatStyleAttributes(docStyle.getDirectiveStringStyle());
*out << formatStyleAttributes(docStyle.getLineStyle());
*out << formatStyleAttributes(docStyle.getSymbolStyle());
/* For output formats which can refer to external styles it is more safe
to use the colour theme's keyword class names, since the language
definitions (which may change during a batch conversion) do not have to define
all keyword classes, that are needed to highlight all input files correctly.
It is ok for RTF to use the language definition's class names, because RTF
does not refer to external styles.
We cannot use the theme's class names, because KSIterator returns an
alphabetically ordered list, which is not good because RTF is dependent
on the order. We access the keyword style with an ID, which is calculated
ignoring the alphabetic order.
*/
vector<string> keywordClasses = langInfo.getKeywordClasses();
for (unsigned int i=0;i<keywordClasses.size();i++){
*out << formatStyleAttributes(docStyle.getKeywordStyle(keywordClasses[i]));
}
*out << "}\n{\\info }\\paperw11900\\paperh16820\\margl560\\margr560\\margt840"
<< "\\margb840\\widowctrl\\ftnbj\\aenddoc\\formshade \\fet0\\sectd"
<< "\\linex0\\endnhere\\plain\\f1\\fs20\n\\pard ";
processRootState();
*out << "}}"<<endl;
}
string RtfGenerator::getFooter()
{
return string();
}
/** Gibt RTF-Code der Sonderzeichen zurueck */
string RtfGenerator::maskCharacter(unsigned char c)
{
switch (c)
{
case '}' :
case '{' :
case '\\' :
{
string m;
m="\\";
return m+=c;
}
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
string m;
m="{";
m+=c;
m+="}";
return m;
}
break;
case AUML_LC:
return "\\'e4";
break;
case OUML_LC:
return "\\'f6";
break;
case UUML_LC:
return "\\'fc";
break;
case AUML_UC:
return "\\'c4";
break;
case OUML_UC:
return "\\'d6";
break;
case UUML_UC:
return "\\'dc";
break;
case AACUTE_LC:
return "\\'e1";
break;
case EACUTE_LC:
return "\\'e9";
break;
case OACUTE_LC:
return "\\'f3";
break;
case UACUTE_LC:
return "\\'fa";
break;
case AGRAVE_LC:
return "\\'e0";
break;
case EGRAVE_LC:
return "\\'e8";
break;
case OGRAVE_LC:
return "\\'f2";
break;
case UGRAVE_LC:
return "\\'f9";
break;
case AACUTE_UC:
return "\\'c1";
break;
case EACUTE_UC:
return "\\'c9";
break;
case OACUTE_UC:
return "\\'d3";
break;
case UACUTE_UC:
return "\\'da";
break;
case AGRAVE_UC:
return "\\'c0";
break;
case EGRAVE_UC:
return "\\'c8";
break;
case OGRAVE_UC:
return "\\'d2";
break;
case UGRAVE_UC:
return "\\'d9";
break;
case SZLIG:
return "\\'df";
break;
// skip first byte of multibyte chracters
/* #ifndef _WIN32
case 195:
return string("");
break;
#endif*/
default :
{
string m;
return m += c;
}
}
}
string RtfGenerator::getMatchingOpenTag(unsigned int styleID){
return getOpenTag(KEYWORD+styleID,
docStyle.getKeywordStyle(langInfo.getKeywordClasses()[styleID]));
}
string RtfGenerator::getMatchingCloseTag(unsigned int styleID){
return getCloseTag(docStyle.getKeywordStyle(langInfo.getKeywordClasses()[styleID]));
}
}